package insight.jsp.custom.taglib;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.io.IOUtils;

public class PageHeaderTagHandler extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String incTiny;
	private String incJeasy;
	private String incXCharts;
	private String incFC;
	private String incKendo;
	private String incAll;

	@Override
	public int doStartTag() throws JspException {
		try {
			boolean incTinyBool = incTiny != null ? incTiny
					.equalsIgnoreCase("yes") : false;
			boolean incAllBool = incAll != null ? incAll
					.equalsIgnoreCase("yes") : false;
			boolean incJeasyBool = incJeasy != null ? incJeasy
					.equalsIgnoreCase("yes") : false;
			boolean incXChartsBool = incXCharts != null ? incXCharts
					.equalsIgnoreCase("yes") : false;
			boolean incFCBool = incFC != null ? incFC.equalsIgnoreCase("yes")
					: false;
			boolean incKendoBool = incKendo != null ? incKendo.equalsIgnoreCase("yes")
					: false;
			JspWriter out = pageContext.getOut();
			InputStream in = this.getClass().getClassLoader()
					.getResourceAsStream("base.inc");
			StringWriter writer = new StringWriter();
			IOUtils.copy(in, writer, "UTF-8");
			String headerStr = writer.toString();
			in.close();
			writer.close();

			if (incAllBool || incTinyBool) {
				in = this.getClass().getClassLoader()
						.getResourceAsStream("tinymce.inc");
				writer = new StringWriter();
				IOUtils.copy(in, writer, "UTF-8");
				headerStr += "\n" + writer.toString();
			}
			if (incAllBool || incJeasyBool) {
				in = this.getClass().getClassLoader()
						.getResourceAsStream("jEasy.inc");
				writer = new StringWriter();
				IOUtils.copy(in, writer, "UTF-8");
				headerStr += "\n" + writer.toString();
			}
			if (incAllBool || incXChartsBool) {
				in = this.getClass().getClassLoader()
						.getResourceAsStream("xCharts.inc");
				writer = new StringWriter();
				IOUtils.copy(in, writer, "UTF-8");
				headerStr += "\n" + writer.toString();
			}
			if (incAllBool || incFCBool) {
				in = this.getClass().getClassLoader()
						.getResourceAsStream("fullCalendar.inc");
				writer = new StringWriter();
				IOUtils.copy(in, writer, "UTF-8");
				headerStr += "\n" + writer.toString();
			}
			if (incAllBool || incKendoBool) {
				in = this.getClass().getClassLoader()
						.getResourceAsStream("kendo.inc");
				writer = new StringWriter();
				IOUtils.copy(in, writer, "UTF-8");
				headerStr += "\n" + writer.toString();
			}
			out.println(headerStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public String getIncTiny() {
		return incTiny;
	}

	public void setIncTiny(String incTiny) {
		this.incTiny = incTiny;
	}

	public String getIncJeasy() {
		return incJeasy;
	}

	public void setIncJeasy(String incJeasy) {
		this.incJeasy = incJeasy;
	}

	public String getIncXCharts() {
		return incXCharts;
	}

	public void setIncXCharts(String incXCharts) {
		this.incXCharts = incXCharts;
	}

	public String getIncFC() {
		return incFC;
	}

	public void setIncFC(String incFC) {
		this.incFC = incFC;
	}

	public String getIncAll() {
		return incAll;
	}

	public void setIncAll(String incAll) {
		this.incAll = incAll;
	}

	public String getIncKendo() {
		return incKendo;
	}

	public void setIncKendo(String incKendo) {
		this.incKendo = incKendo;
	}

}